﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Xml.Serialization;

namespace KSS.Generator.Configuration
{
    [XmlRootAttribute("DbmlToClassConfiguration",
            Namespace = "http://schema.kss.com/DbmlToClassConfiguration/0.1",
            IsNullable = false)]
    public class GeneratorSetting : ICloneable, ICustomTypeDescriptor
    {
        #region [ General ]
        [XmlAttribute]
        public string Version = "0.1";

        [XmlAttribute]
        public string DatabaseFilePath = string.Empty;

        [XmlAttribute]
        public string TemplateFolderPath = string.Empty;

        [XmlAttribute]
        public string GeneratedFolderPath = string.Empty;

        [XmlAttribute]
        public DateTime CreateDate = DateTime.Now;

        [XmlAttribute]
        public DateTime LastModifyDate = DateTime.Now;
        #endregion

        private List<ProjectSetting> _projects;

        [Browsable(false)]
        [XmlElement(ElementName = "Project", Type = typeof(ProjectSetting))]
        public List<ProjectSetting> Projects
        {
            get
            {
                if (_projects == null)
                {
                    _projects = new List<ProjectSetting>();
                }

                return _projects;
            }
            set
            {
                _projects = value;
            }
        }

        /// <summary>
        /// Get setting of a project
        /// </summary>
        /// <param name="projectName"></param>
        /// <returns></returns>
        public ProjectSetting GetProjectSetting(string projectName)
        {
            // Find project settings
            ProjectSetting project = Projects.Find(
                        delegate(ProjectSetting p)
                        {
                            return (p.Name.Equals(projectName, StringComparison.OrdinalIgnoreCase));
                        });

            return project;
        }

        #region ICloneable Members

        public object Clone()
        {
            GeneratorSetting setting = new GeneratorSetting();
            setting.Version = Version;
            setting.DatabaseFilePath = DatabaseFilePath;
            setting.TemplateFolderPath = TemplateFolderPath;
            setting.GeneratedFolderPath = GeneratedFolderPath;
            setting.CreateDate = CreateDate;
            setting.LastModifyDate = LastModifyDate;
            setting.Projects = new List<ProjectSetting>();

            foreach (ProjectSetting project in Projects)
            {
                setting.Projects.Add((ProjectSetting)project.Clone());
            }

            return setting;
        }

        #endregion

        #region ICustomTypeDescriptor Members


        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return GetProperties();
        }

        public PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection projectDescriptors = new PropertyDescriptorCollection(null);

            ProjectSettingPropertyDescriptor descriptor = null;
            foreach (ProjectSetting project in Projects)
            {
                descriptor = new ProjectSettingPropertyDescriptor(project);
                projectDescriptors.Add(descriptor);
            }

            return projectDescriptors;
        }

        #endregion
    }
}